<template>
  <div class="common-page page-work-order" v-loading.fullscreen="loading">
    <div class="common-page__head">
      <comp-paths
        class="route"
        :paths="['/work', '/work/orders/type0', $route.fullPath]"
      ></comp-paths>
      <div class="title" v-if="order">
        <div class="text">{{ order.title }}</div>
        <div class="flag" v-for="tag in order.tags" :key="tag.id">
          {{ tag.name }}
        </div>
      </div>
      <div class="title" v-else></div>
    </div>
    <div
      class="common-page__body common-form"
      style="display: flex"
      v-if="order"
    >
      <div class="rows" style="flex: 3 1 0; padding-right: 0">
        <div class="row row--head">
          <div class="name">基本信息</div>
          <div class="value"></div>
        </div>
        <div class="row row--3x">
          <div class="name">
            <span v-if="order.company.type === 'company'">企业</span>
            <span v-else-if="order.company.type === 'project'">项目</span>
            <span v-else-if="order.company.type === 'area'">园区</span>
            <span>名称</span>
          </div>
          <div class="value">{{ order.company.name }}</div>
        </div>
        <div class="row row--3x">
          <div class="name">事项标题</div>
          <div class="value">{{ order.title }}</div>
        </div>
        <div class="row row--3x">
          <div class="name">事项描述</div>
          <div class="value" style="white-space: pre-wrap">
            {{ order.content }}
          </div>
        </div>
        <div class="row row--3x">
          <div class="name">事项附件</div>
          <div class="value">
            <div style="padding: 1px 0">
              <comp-files-viewer
                :files="order.files"
                v-if="order.files.length > 0"
              ></comp-files-viewer>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="name">事项分类</div>
          <div class="value">
            <span class="text" v-for="tag in order.tags" :key="tag.id">{{
              tag.name
            }}</span>
          </div>
        </div>
        <div class="row">
          <div class="name">事项层面</div>
          <div class="value">
            <div v-if="order.level == 1">县级</div>
            <div v-else-if="order.level == 2">市级</div>
            <div v-else-if="order.level == 3">自治区级</div>
            <div v-else>
              <span v-if="order.dept.county.id">县级</span>
              <span v-else-if="order.dept.city.id">市级</span>
              <span v-else-if="order.dept.province.id">自治区级</span>
            </div>
          </div>
        </div>
        <div class="row">
          <div class="name">事项状态</div>
          <div class="value">
            <span
              class="el-status--info"
              v-if="order.status === 'created'"
            ></span>
            <span
              class="el-status--danger"
              v-else-if="
                order.status === 'published' || order.status === 'return'
              "
            ></span>
            <span
              class="el-status--warning"
              v-else-if="order.status === 'handling'"
            ></span>
            <span
              class="el-status--success"
              v-else-if="order.status === 'finished'"
            ></span>
            <span>{{ order.statusText }}</span>
          </div>
        </div>
        <div class="row row--2x">
          <div class="name">提交人</div>
          <div class="value">
            <span>{{ order.creator.name }}</span>
            <span style="margin-left: 0.5em; color: #aaa">{{
              order.creator.number
            }}</span>
          </div>
        </div>
        <div class="row">
          <div class="name">提交时间</div>
          <div class="value">{{ order.createdAt }}</div>
        </div>
        <!--        <div class="row row&#45;&#45;2x">-->
        <!--          <div class="name">负责工作队</div>-->
        <!--          <div class="value">-->
        <!--            <span>{{ order.hand.name }}</span>-->
        <!--          </div>-->
        <!--        </div>-->
        <!--        <div class="row">-->
        <!--          <div class="name">负责服务专员</div>-->
        <!--          <div class="value">-->
        <!--            <span>{{ order.handler.name }}</span>-->
        <!--            <span style="margin-left: 0.5em; color: #aaa">{{ order.handler.number }}</span>-->
        <!--          </div>-->
        <!--        </div>-->
        <div class="row row--2x">
          <div class="name">关注数量</div>
          <div class="value">
            <span class="text">{{ order.likes }}人</span>
            <span class="button" @click="getOrderLikers()">查看</span>
            <span class="button" @click="addOrderLiker()" v-if="order.liked"
              >取消关注</span
            >
            <span class="button" @click="addOrderLiker()" v-else>关注</span>
          </div>
        </div>
        <div class="row">
          <div class="name">办结时间</div>
          <div class="value">
            <span>{{ order.finishedAt }}</span>
          </div>
        </div>
        <div class="row">
          <div class="name">评价人</div>
          <div class="value">
            <span>{{ order.evaluatedCreator.name }}</span>
            <span style="margin-left: 0.5em; color: #aaa">{{
              order.evaluatedCreator.number
            }}</span>
          </div>
        </div>

        <div class="row" v-if="order.fwxz">
          <div class="name">调研服务组：</div>
          <div class="value">
            <span>{{ order.fwxz }}</span>
          </div>
        </div>
        <div class="row">
          <div class="name">评价打分</div>
          <div class="value">
            <span v-if="order.evaluated === 'yes'"
              >{{ order.evaluatedStars }}分</span
            >
          </div>
        </div>
        <div class="row">
          <div class="name">办结类型：</div>
          <div class="value">
            <span v-if="order.bjlx == '0'">线上办结</span>
            <span v-if="order.bjlx == '1'">现场办结</span>
          </div>
        </div>
        <div class="row">
          <div class="name">办成情况：</div>
          <div class="value">
            <span v-if="order.bcqk == '0'">未办成</span>
            <span v-if="order.bcqk == '1'">已办成</span>
          </div>
        </div>
        <div class="row">
          <div class="name">问题来源：</div>
          <div class="value">
            <span v-if="order.wtly == '0'">自治区调研</span>
            <span v-if="order.wtly == '1'">市/县调研</span>
          </div>
        </div>
        <div class="row row--3x">
          <div class="name">评价内容</div>
          <div class="value">
            <span v-if="order.evaluated === 'yes'">{{
              order.evaluatedContent
            }}</span>
          </div>
        </div>
      </div>
      <div
        class="rows"
        style="
          flex: 2 1 0;
          flex-wrap: nowrap;
          flex-direction: column;
          padding-right: 0;
        "
      >
        <div class="row row--head">
          <div class="name">处理过程信息</div>
          <div class="value"></div>
        </div>
        <div
          class="row row--3x"
          style="flex: 1 1 0; overflow-x: hidden; overflow-y: auto"
        >
          <div class="value">
            <comp-order-tracks
              :order="order"
              @updated="init()"
            ></comp-order-tracks>
          </div>
        </div>
      </div>
    </div>
    <!--        <div class="common-page__foot" v-if="editing || starting || accepting || checking || handling || relaying || reviewing || finishing || revoking || returning || deleting">-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="setOrder()" v-if="editing">编辑</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="startOrderTrack()" v-if="starting">拟办</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="acceptOrderTrack()" v-if="accepting">受理</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="checkOrderTrack()" v-if="checking">审核</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="upOrderTrack()" v-if="handling && (order.dept.county.id || order.dept.city.id)">上报</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="downOrderTrack()" v-if="handling && order.dept.city.id && !order.dept.county.id">下派</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="relayOrderTrack()" v-if="handling">转派</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="replyOrderTrack()" v-if="handling">答复</el-button>-->
    <!--            <el-button type="plain" size="small" icon="el-icon-close" @click="revokeOrderTrackSign()" v-if="relaying">撤回转派</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="reviewOrderTrack()" v-if="reviewing">审查</el-button>-->
    <!--            <el-button type="primary" size="small" icon="el-icon-check" @click="finishOrderTrack()" v-if="finishing">办结</el-button>-->
    <!--            <el-button type="plain" size="small" icon="el-icon-close" @click="revokeOrderTrack()" v-if="revoking">撤回</el-button>-->
    <!--            <el-button type="plain" size="small" icon="el-icon-close" @click="returnOrderTrack()" v-else-if="returning">退回</el-button>-->
    <!--            <el-button type="danger" size="small" icon="el-icon-delete" @click="delOrder()" v-if="deleting">删除</el-button>-->
    <!--        </div>-->
    <div
      class="common-page__foot"
      v-if="
        editing ||
        accepting ||
        handling ||
        finishing ||
        revoking ||
        returning ||
        deleting ||
        uping ||
        handling2
      "
    >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="setOrder()"
        v-if="editing"
        >编辑</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="relayOrderTrack()"
        v-if="accepting || finishing"
        >转派
      </el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="downOrderTrack()"
        v-if="downing && order.level > 1"
        >下派</el-button
      >
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="upOrderTrack()"
        v-if="uping"
        >上报
      </el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="replyOrderTrackSign()"
        v-if="handling"
        >答复
      </el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="replyOrderTrackSign()"
        v-if="handling2"
        >再次答复
      </el-button>
      <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="finishOrderTrack()"
        v-if="finishing"
        >办结
      </el-button>
       <!-- 8月2号需求，隐藏按钮 -->
      <!-- <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="finishOrderTrackybc()"
        v-if="finishing && bcqk == '0'"
        >已办成
      </el-button> -->
      <!-- <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="finishOrderTrackybc()"
        v-if="sucesssbcqk && bcqk == '0'"
        >已办成
      </el-button> -->
      <el-button
        type="plain"
        size="small"
        icon="el-icon-close"
        @click="revokeOrderTrack()"
        v-if="revoking"
        >撤回
      </el-button>
      <el-button
        type="plain"
        size="small"
        icon="el-icon-close"
        @click="returnOrderTrack()"
        v-else-if="returning"
        >退回
      </el-button>
      <el-button
        type="danger"
        size="small"
        icon="el-icon-delete"
        @click="delOrder()"
        v-if="deleting"
        >删除</el-button
      >
    </div>
    <div class="common-page__foot" v-else>
       <!-- 8月2号需求，隐藏按钮 -->
      <!-- <el-button
        type="primary"
        size="small"
        icon="el-icon-check"
        @click="finishOrderTrackybc()"
        v-if="bcqk == '0'"
        >已办成
      </el-button> -->
    </div>
    <comp-dialog title="查看关注" width="60vw" height="80vh" ref="orderLiker">
      <comp-order-likers
        style="height: 100%"
        ref="orderLikerList"
        :orderId="orderId"
      >
        <template #foot="scope">
          <el-button
            type="plain"
            size="small"
            icon="el-icon-close"
            @click="getOrderLikers('end')"
            >关闭</el-button
          >
        </template>
      </comp-order-likers>
    </comp-dialog>
    <comp-dialog title="编辑事项" ref="order" @close="setOrder('end')">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order"
      >
        <el-form-item label="问题来源：">
          <el-select
            v-model="order.wtly"
            style="width: 600px"
            placeholder="请选择"
            @change="setOrder('validate')"
          >
            <el-option key="0" label="自治区调研" value="0"></el-option>
            <el-option key="1" label="市/县调研" value="1"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问题层级：" required>
          <el-select
            v-model="order.level"
            style="width: 600px"
            placeholder="请选择"
            @change="setOrder('order-dept')"
          >
            <el-option key="1" label="区县级" value="1"></el-option>
            <el-option key="2" label="市级" value="2"></el-option>
            <el-option key="3" label="自治区级" value="3"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="调研服务组：" required v-if="order.wtly === '0'">
          <el-select
            v-model="order.fwxz"
            style="width: 600px"
            placeholder="请选择"
            @change="setOrder('validate')"
          >
            <el-option
              key="17"
              label="市县自主调研"
              value="市县自主调研"
            ></el-option>

            <el-option key="0" label="第一组" value="第一组"></el-option>
            <el-option key="1" label="第二组" value="第二组"></el-option>
            <el-option key="2" label="第三组" value="第三组"></el-option>
            <el-option key="3" label="第四组" value="第四组"></el-option>
            <el-option key="4" label="第五组" value="第五组"></el-option>
            <el-option key="5" label="第六组" value="第六组"></el-option>
            <el-option key="6" label="第七组" value="第七组"></el-option>
            <el-option key="7" label="第八组" value="第八组"></el-option>
            <el-option key="8" label="第九组" value="第九组"></el-option>
            <el-option key="9" label="第十组" value="第十组"></el-option>
            <el-option key="10" label="第十一组" value="第十一组"></el-option>
            <el-option key="11" label="第十二组" value="第十二组"></el-option>
            <el-option key="12" label="第十三组" value="第十三组"></el-option>
            <el-option key="13" label="第十四组" value="第十四组"></el-option>
            <el-option key="14" label="第十五组" value="第十五组"></el-option>
            <el-option key="15" label="第十六组" value="第十六组"></el-option>
            <el-option key="16" label="第十七组" value="第十七组"></el-option>
            <el-option key="17" label="厅局调研" value="厅局调研"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="事项标题：" required>
          <el-input
            type="textarea"
            v-model="order.title"
            style="width: 600px"
            placeholder="请输入"
            :maxlength="100"
            :rows="2"
            resize="none"
            :readonly="loading"
            @change="setOrder('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="事项描述：" required>
          <el-input
            type="textarea"
            v-model="order.content"
            style="width: 600px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="10"
            resize="none"
            :readonly="loading"
            @change="setOrder('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="事项分类：" required>
          <div style="width: 600px">
            <el-checkbox
              v-for="tag in tags"
              :key="tag.id"
              v-model="tag.selected"
              style="margin-right: 20px"
              :label="tag.name"
              :disabled="order.tags.length >= 1 && !tag.selected"
              @change="setOrder('validate')"
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item label="事项附件：">
          <div style="width: 600px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in order.files"
              :key="index"
              disable-transitions
              closable
              @close="setOrder('order-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="setOrder('order-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="setOrder('order-file-upload', 'document')"
            >选择文档
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="setOrder('submit')"
            >保存</el-button
          >
          <el-button type="plain" icon="el-icon-close" @click="setOrder('end')"
            >放弃</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项拟办" ref="orderTrackStartForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="接收人：" required>
          <el-select
            v-model="track.handler.id"
            style="width: 480px"
            placeholder="请选择"
            @change="startOrderTrack('validate')"
          >
            <el-option
              v-for="data in handlers"
              :key="data.id"
              :label="data.flag"
              :value="data.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="拟办意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="startOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="拟办附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="startOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="startOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="startOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="startOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="startOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <!--    <comp-dialog title="事项转派" ref="orderTrackAcceptForm">-->
    <!--      <el-form size="small" label-width="98px" style="margin-right: 20px" @submit.native.prevent v-if="order && track">-->
    <!--         <el-form-item label="部门：" required>-->
    <!--          <el-select v-model="track.deptId" filterable style="width: 480px" placeholder="请选择"-->
    <!--                     @change="acceptOrderTrack('validate')">-->
    <!--            <el-option v-for="data in deptList" :key="data.id" :label="data.name" :value="data.id"></el-option>-->
    <!--          </el-select>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="接收人：" required>-->
    <!--          -->
    <!--          <el-select v-model="track.handler.id" style="width: 480px" placeholder="请选择"-->
    <!--                     @change="acceptOrderTrack('validate')">-->
    <!--            <el-option v-for="data in handlers" :key="data.id" :label="data.client.name" :value="data.client.id"></el-option>-->
    <!--          </el-select>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="转派意见：">-->
    <!--          <el-input type="textarea" v-model="track.content" style="width: 480px" placeholder="请输入" :maxlength="1000"-->
    <!--                    :rows="5" resize="none" :readonly="loading" @change="acceptOrderTrack('validate')"></el-input>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="转派附件：">-->
    <!--          <div style="width: 480px">-->
    <!--            <el-tag style="margin: 0 10px 10px 0" v-for="(file, index) in track.files" :key="index" disable-transitions-->
    <!--                    closable @close="acceptOrderTrack('track-file-delete', file)">{{ file.name }}-->
    <!--            </el-tag>-->
    <!--          </div>-->
    <!--          <el-button type="plain" icon="el-icon-plus" @click="acceptOrderTrack('track-file-upload', 'image')">选择图片-->
    <!--          </el-button>-->
    <!--          <el-button type="plain" icon="el-icon-plus" @click="acceptOrderTrack('track-file-upload', 'document')">-->
    <!--            选择文档-->
    <!--          </el-button>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item label="抄送给：" v-for="office in offices" :key="office.id">-->
    <!--          <el-tag type="info">{{ office.name }}</el-tag>-->
    <!--          <div style="width: 480px">-->
    <!--            <el-checkbox style="margin: 10px 10px 0 0" v-for="depment in office.depments" :key="depment.id"-->
    <!--                         v-model="depment.selected" :label="depment.client.name" border></el-checkbox>-->
    <!--          </div>-->
    <!--        </el-form-item>-->
    <!--        <el-form-item>-->
    <!--          <el-button type="primary" icon="el-icon-check" @click="acceptOrderTrack('submit')">确定</el-button>-->
    <!--          <el-button type="plain" icon="el-icon-close" @click="acceptOrderTrack('end')">取消</el-button>-->
    <!--        </el-form-item>-->
    <!--      </el-form>-->
    <!--    </comp-dialog>-->
    <comp-dialog title="事项审核" ref="orderTrackCheckForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="接收人：" required>
          <el-select
            v-model="track.handler.id"
            style="width: 480px"
            placeholder="请选择"
            @change="checkOrderTrack('validate')"
          >
            <el-option
              v-for="data in handlers"
              :key="data.id"
              :label="data.flag"
              :value="data.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="checkOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="审核附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="checkOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="checkOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="checkOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="checkOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="checkOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项上报" ref="orderTrackUpForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="上报层级：" required>
          <el-select
            v-model="track.lev"
            style="width: 480px"
            placeholder="请选择"
            @change="upOrderTrack('track-dept')"
          >
            <el-option
              key="1"
              label="县级"
              value="1"
              v-if="1 > this.cenji"
            ></el-option>
            <el-option
              key="2"
              label="市级"
              value="2"
              v-if="2 > this.cenji"
            ></el-option>
            <el-option
              key="3"
              label="自治区"
              value="3"
              v-if="3 > this.cenji"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="接收人：" required>
          <el-select v-model="track.handler.id" style="width: 480px" placeholder="请选择"
                     @change="upOrderTrack('validate')">
            <el-option v-for="data in handlers" :key="data.id" :label="data.flag" :value="data.id"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="上报意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="upOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="上报附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="upOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="upOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="upOrderTrack('track-file-upload', 'document')"
            >选择文档
          </el-button>
        </el-form-item>
        <!-- <el-form-item label="抄送给：" v-for="office in offices" :key="office.id">
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox style="margin: 10px 10px 0 0" v-for="depment in office.depments" :key="depment.id"
                         v-model="depment.selected" :label="depment.client.name" border></el-checkbox>
          </div>
        </el-form-item> -->
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="upOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="upOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项下派" ref="orderTrackDownForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <!-- <el-form-item label="下派给：" required>
          <el-select v-model="track.dept.id" style="width: 480px" placeholder="请选择"
                     @change="downOrderTrack('track-dept')">
            <el-option v-for="data in depts" :key="data.id" :label="data.name" :value="data.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接收人：" required>
          <el-select v-model="track.handler.id" style="width: 480px" placeholder="请选择"
                     @change="downOrderTrack('validate')">
            <el-option v-for="data in handlers" :key="data.id" :label="data.flag" :value="data.id"></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="下派层级：" required>
          <el-select
            v-model="track.lev"
            style="width: 480px"
            placeholder="请选择"
            @change="upOrderTrack('track-dept')"
          >
            <el-option
              key="1"
              label="县级"
              value="1"
              v-if="1 < this.cenji"
            ></el-option>
            <el-option
              key="2"
              label="市级"
              value="2"
              v-if="2 < this.cenji"
            ></el-option>
            <el-option
              key="3"
              label="自治区"
              value="3"
              v-if="3 < this.cenji"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="下派意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="downOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="下派附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="downOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="downOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="downOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="downOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="downOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项转派" ref="orderTrackRelayForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="转派给：" required>
          <div
            style="width: 360px"
            v-for="(data, index) in track.signs"
            :key="index"
          >
            <el-tag
              style="margin: 0 10px 10px 0; max-width: 250px"
              disable-transitions
              >{{ data.dept.name }}</el-tag
            >
            <el-button
              type="plain"
              icon="el-icon-edit"
              @click="relayOrderTrack('track-sign-start', data)"
            ></el-button>
            <el-button
              type="plain"
              icon="el-icon-delete"
              @click="relayOrderTrack('track-sign-delete', data)"
            ></el-button>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="relayOrderTrack('track-sign-start')"
            >添加转派</el-button
          >
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 360px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="relayOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="relayOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="添加转派" ref="orderTrackRelayFormSign">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track && sign"
      >
        <el-form-item label="转派给：" required>
          <el-select
            v-model="sign.dept.id"
            style="width: 360px"
            placeholder="请输入关键词"
            filterable
            @change="relayOrderTrack('track-sign-dept')"
          >
            <el-option
              v-for="data in depts"
              :key="data.id"
              :label="data.name"
              :value="data.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="接收人：" required>
          <el-select
            v-model="sign.handler.id"
            style="width: 360px"
            placeholder="请选择"
            @change="relayOrderTrack('track-sign-validate')"
          >
            <el-option
              v-for="data in handlers"
              :key="data.id"
              :label="data.flag"
              :value="data.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="转派意见：">
          <el-input
            type="textarea"
            v-model="sign.content"
            style="width: 360px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="relayOrderTrack('track-sign-validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="转派附件：">
          <div style="width: 360px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in sign.files"
              :key="index"
              disable-transitions
              closable
              @close="relayOrderTrack('track-sign-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="relayOrderTrack('track-sign-file-upload', 'image')"
          >
            选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="relayOrderTrack('track-sign-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="relayOrderTrack('track-sign-submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="relayOrderTrack('track-sign-end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项答复" ref="orderTrackReplyForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="接收人：" required>
          <el-select
            v-model="track.handler.id"
            style="width: 480px"
            placeholder="请选择"
            @change="replyOrderTrack('validate')"
          >
            <el-option
              v-for="data in handlers"
              :key="data.id"
              :label="data.flag"
              :value="data.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="答复意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="replyOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="答复附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="replyOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="replyOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="replyOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="replyOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="replyOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项答复" ref="orderTrackSignReplyForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="答复意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="replyOrderTrackSign('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="答复附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="replyOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="replyOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="replyOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="replyOrderTrackSign('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="replyOrderTrackSign('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项审查" ref="orderTrackReviewForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="接收人：" required>
          <el-select
            v-model="track.handler.id"
            style="width: 480px"
            placeholder="请选择"
            @change="reviewOrderTrack('validate')"
          >
            <el-option
              v-for="data in handlers"
              :key="data.id"
              :label="data.name"
              :value="data.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审查意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="reviewOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="审查附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="reviewOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="reviewOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="reviewOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="reviewOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="reviewOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项退回" ref="orderTrackReturnForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="退回意见：">
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="returnOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="退回附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="returnOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="returnOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="returnOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="returnOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="returnOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
    <comp-dialog title="事项办结" ref="orderTrackFinishForm">
      <el-form
        size="small"
        label-width="98px"
        style="margin-right: 20px"
        @submit.native.prevent
        v-if="order && track"
      >
        <el-form-item label="办结意见：" required>
          <el-input
            type="textarea"
            v-model="track.content"
            style="width: 480px"
            placeholder="请输入"
            :maxlength="1000"
            :rows="5"
            resize="none"
            :readonly="loading"
            @change="finishOrderTrack('validate')"
          ></el-input>
        </el-form-item>
        <el-form-item label="办结附件：">
          <div style="width: 480px">
            <el-tag
              style="margin: 0 10px 10px 0"
              v-for="(file, index) in track.files"
              :key="index"
              disable-transitions
              closable
              @close="finishOrderTrack('track-file-delete', file)"
              >{{ file.name }}
            </el-tag>
          </div>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="finishOrderTrack('track-file-upload', 'image')"
            >选择图片
          </el-button>
          <el-button
            type="plain"
            icon="el-icon-plus"
            @click="finishOrderTrack('track-file-upload', 'document')"
          >
            选择文档
          </el-button>
        </el-form-item>
        <el-form-item
          label="抄送给："
          v-for="office in offices"
          :key="office.id"
        >
          <el-tag type="info">{{ office.name }}</el-tag>
          <div style="width: 480px">
            <el-checkbox
              style="margin: 10px 10px 0 0"
              v-for="depment in office.depments"
              :key="depment.id"
              v-model="depment.selected"
              :label="depment.client.name"
              border
            ></el-checkbox>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-check"
            @click="finishOrderTrack('submit')"
            >确定</el-button
          >
          <el-button
            type="plain"
            icon="el-icon-close"
            @click="finishOrderTrack('end')"
            >取消</el-button
          >
        </el-form-item>
      </el-form>
    </comp-dialog>
  </div>
</template>

<script>
import mixin from "../mixin";
import { string, int, deepCopy, selectFile, cache } from "../util";

export default {
  name: "page-work-order",
  title: "事项详情",
  nodes: ["/menu/work"],
  childs: [],
  mixins: [mixin],
  data: () => ({
    cenji: 0,
    loading: false,
    orderId: "",
    bcqk: "",
    order: null,
    editing: false,
    starting: false,
    accepting: false,
    checking: false,
    handling: false,
    handling2: false,
    relaying: false,
    reviewing: false,
    finishing: false,
    sucesssbcqk: false,
    revoking: false,
    returning: false,
    deleting: false,
    downing: false,
    uping: false,
    track: null,
    sign: null,
    depts: [],
    handlers: [],
    ahandlers: [],
    offices: [],
    tags: [],
    deptList: [],
    deptId: "",
  }),
  methods: {
    addOrderLiker() {
      if (this.loading) return;
      let order = this.order;
      this.loading = true;
      this.plugins.user.addOrderLiker(order).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          this.plugins.modal.toast("操作成功！", "success");
          this.init();
        } else {
          this.plugins.modal.toast(
            "操作失败<br>" + res.code + "：" + res.msg,
            "error"
          );
        }
      });
    },
    getOrderLikers(action, data) {
      if (this.loading) return;
      if (!action || action === "start") {
        this.$refs.orderLiker.init();
        this.$nextTick(() => this.$refs.orderLikerList.init());
      } else if (action === "end") {
        this.$refs.orderLiker.close();
      }
    },
    uploadFile(type, file, callback) {
      if (this.loading) return;
      if (type && file) {
        let timer = null;
        let progress = (event) =>
          (timer =
            clearTimeout(timer) ||
            setTimeout(
              () =>
                this.plugins.modal.toast(
                  "上传进度：" + event.percent + "%",
                  "info"
                ),
              200
            ));
        this.loading = true;
        this.plugins.user.uploadFile(file, type, progress).then((res) => {
          this.loading = false;
          if (res.code === 0) string(() => callback(res.data));
          else
            this.plugins.modal.alert(
              "上传失败",
              res.code + "：" + res.msg,
              "error"
            );
        });
      } else if (type === "image") {
        selectFile("image/*").then((file) => {
          if (file.size > 5 * 1048576)
            this.plugins.modal.toast(
              "图片过大，不能大于5MB哦！请重新选择。",
              "error"
            );
          else this.uploadFile(type, file, callback);
        });
      } else if (type === "document") {
        selectFile(".xls,.xlsx,.doc,.docx,.pdf").then((file) => {
          if (file.size > 10 * 1048576)
            this.plugins.modal.toast(
              "文档过大，不能大于10MB哦！请重新选择。",
              "error"
            );
          else this.uploadFile(type, file, callback);
        });
      }
    },
    setOrder(action, data) {
      if (this.loading) return;
      let order = this.order;
      let tags = this.tags;
      if (action === "start" || !action) {
        this.loading = true;
        this.plugins.user
          .call(async () => {
            if (tags.length > 0) {
            } else
              tags = await this.plugins.user
                .getOrderOfTags()
                .then((res) => (res.code === 0 ? res.data : []));
            return { code: 0 };
          })
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.tags = tags;
              this.tags.forEach(
                (tag) =>
                  (tag.selected = order.tags
                    .map((tag) => tag.id)
                    .includes(tag.id))
              );
              this.setOrder("validate");
              this.$refs.order.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        order.title = string(order.title).trim();
        error = error || (order.title ? "" : "请先输入事项标题！");
        order.content = string(order.content).trim();
        error = error || (order.content ? "" : "请先输入事项描述！");
        order.tags = tags.filter((tag) => tag.selected);

        error = error || (order.tags.length == 1 ? "" : "事项分类必填！");


        order.level = string(order.level).trim();
        order.fwxz = string(order.fwxz).trim();
        if (order.wtly === "0") {
          error = error || (order.fwxz ? "" : "请先选择调研服务组！");
        }

        error = error || (order.level ? "" : "请先选择问题层级！");
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "order-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            order.files.push(file);
            this.setOrder("validate");
          });
      } else if (action === "order-file-delete") {
        let file = data;
        let index = order.files.indexOf(file);
        if (index > -1) order.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.setOrder("validate", true)) return;
        this.loading = true;
        this.plugins.user.setOrder(order).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("保存成功！", "success");
            this.setOrder("end");
          } else {
            this.plugins.modal.alert(
              "保存失败",
              res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.order.close();
        this.init();
      }
    },
    startOrderTrack(action, data) {
      if (this.loading) return;
      if (!this.starting) return;
      let order = this.order;
      let track = this.track;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDeptDepments(
                order.dept
              );

              if (res.code === 0)
                handlers = res.data.reduce((handlers, depment) => {
                  depment.client.flag =
                    depment.client.name + " - " + depment.duty;
                  if (
                    depment.role === "manager" &&
                    depment.client.banned === "not"
                  )
                    handlers.push(depment.client);
                  return handlers;
                }, []);
              else handlers = [];
              return res;
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.track.handler = this.plugins.user.formatClient();
              this.handlers = handlers;
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.startOrderTrack("validate");
              this.$refs.orderTrackStartForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        error = error || (track.handler.id ? "" : "请先选择接收人！");
        track.content = string(track.content).trim();
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.startOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.startOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.startOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.startOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackStartForm.close();
      }
    },
    acceptOrderTrack(action, data) {
      if (this.loading) return;
      if (!this.accepting) return;
      let order = this.order;
      let track = this.track;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              console.log(this.deptId);
              let res = await this.plugins.user.getOrderDeptDepments(
                this.deptId
              );
              if (res.code === 0)
                handlers = res.data.reduce((clients, depment) => {
                  depment.client.flag =
                    depment.client.name + " - " + depment.duty;
                  if (
                    depment.role === "manager" &&
                    depment.client.banned === "not"
                  )
                    clients.push(depment.client);
                  return clients;
                }, []);
              else handlers = [];
              return res;
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              this.deptList = res.data;
              track.deptId = string(track.deptId).trim();

              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;

              this.track.handler = this.plugins.user.formatClient();

              this.track.deptList = this.plugins.user.formatClient();
              console.log(this.track);
              this.handlers = handlers;
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.acceptOrderTrack("validate");
              this.$refs.orderTrackAcceptForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        console.log(this.track.deptId);
        let i = this.plugins.user
          .getOrderDeptDepments(this.track.deptId)
          .then((res) => {
            this.handlers = res.data;
          });
        error = error || (track.handler.id ? "" : "请先选择接收人！");
        track.content = string(track.content).trim();
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.acceptOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.acceptOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.acceptOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.acceptOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackAcceptForm.close();
      }
    },
    checkOrderTrack(action, data) {
      if (this.loading) return;
      if (!this.checking) return;
      let order = this.order;
      let track = this.track;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDeptDepments(
                order.dept
              );
              if (res.code === 0)
                handlers = res.data.reduce((clients, depment) => {
                  depment.client.flag =
                    depment.client.name + " - " + depment.duty;
                  if (
                    depment.primary === "yes" &&
                    depment.client.banned === "not"
                  )
                    clients.push(depment.client);
                  return clients;
                }, []);
              else handlers = [];
              return res;
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.track.handler = this.plugins.user.formatClient();
              this.handlers = handlers;
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.checkOrderTrack("validate");
              this.$refs.orderTrackCheckForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        error = error || (track.handler.id ? "" : "请先选择接收人！");
        track.content = string(track.content).trim();
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.checkOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.checkOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.checkOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.checkOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackCheckForm.close();
      }
    },
    upOrderTrack(action, data) {
      if (this.loading) return;
      //if (!this.handling) return;
      let order = this.order;
      let track = this.track;
      let depts = this.depts;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                depts = res.data.filter(
                  (dept) => dept.id === order.dept.dept.id
                );
              else depts = [];
              return res;
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.track.dept = this.plugins.user.formatDept();
              this.track.handler = this.plugins.user.formatClient();
              this.depts = depts;
              this.handlers = [];
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.upOrderTrack("validate");
              this.$refs.orderTrackUpForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        // let error = "";
        // track.dept.id = string(() => depts.find((dept) => dept.id === track.dept.id).id);
        // error = error || (track.dept.id ? "" : "请先选择上报给哪个队伍！");
        // track.handler.id = string(() => handlers.find((handler) => handler.id === track.handler.id).id);
        // error = error || (track.handler.id ? "" : "请先选择接收人！");
        // track.content = string(track.content).trim();
        // track.lev = string(track.lev).trim();
        // track.copytos = offices.map((office) => office.depments).flat().reduce((copytos, depment) => {
        //   let index = copytos.findIndex((copyto) => copyto.id === depment.client.id);
        //   if (index === -1) depment.selected && copytos.push(depment.client);
        //   return copytos;
        // }, []);
        // if (data) this.plugins.modal.toast(error, "error");
        // return !error;
      } else if (action === "track-dept") {
        // this.loading = true;
        // this.plugins.user.getOrderDeptDepments(track.dept).then((res) => {
        //   this.loading = false;
        //   if (res.code === 0) {
        //     this.handlers = res.data.reduce((clients, depment) => {
        //       depment.client.flag = depment.client.name + " - " + depment.duty;
        //       if (depment.primary === "yes" && depment.client.banned === "not") clients.push(depment.client);
        //       return clients;
        //     }, []);
        //     this.upOrderTrack("validate");
        //   } else {
        //     track.dept.id = "";
        //     track.handler.id = "";
        //     this.handlers = [];
        //     this.upOrderTrack("validate");
        //     this.plugins.modal.toast("加载失败<br>" + res.code + "：" + res.msg, "error");
        //   }
        // });
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.upOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        // if (!this.upOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.upOrderTrackV3(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.upOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackUpForm.close();
      }
    },
    downOrderTrack(action, data) {
      if (this.loading) return;
      //if (!this.handling) return;
      let order = this.order;
      let track = this.track;
      let depts = this.depts;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                depts = res.data.filter(
                  (dept) => dept.dept.id === order.dept.id
                );
              else depts = [];
              return res;
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.track.dept = this.plugins.user.formatDept();
              this.track.handler = this.plugins.user.formatClient();
              this.depts = depts;
              this.handlers = [];
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.downOrderTrack("validate");
              this.$refs.orderTrackDownForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        track.dept.id = string(
          () => depts.find((dept) => dept.id === track.dept.id).id
        );
        // error = error || (track.dept.id ? "" : "请先选择下派给哪个队伍！");
        track.handler.id = string(
          () => handlers.find((handler) => handler.id === track.handler.id).id
        );
        // error = error || (track.handler.id ? "" : "请先选择接收人！");
        track.content = string(track.content).trim();
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-dept") {
        this.loading = true;
        this.plugins.user.getOrderDeptDepments(track.dept).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.handlers = res.data.reduce((clients, depment) => {
              depment.client.flag = depment.client.name + " - " + depment.duty;
              if (depment.primary === "yes" && depment.client.banned === "not")
                clients.push(depment.client);
              return clients;
            }, []);
            this.downOrderTrack("validate");
          } else {
            track.dept.id = "";
            track.handler.id = "";
            this.handlers = [];
            this.downOrderTrack("validate");
            this.plugins.modal.toast(
              "加载失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.downOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.downOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.downOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.downOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackDownForm.close();
      }
    },
    relayOrderTrack(action, data) {
      if (this.loading) return;
      if (!this.handling) return;
      let order = this.order;
      let track = this.track;
      let sign = this.sign;
      let depts = this.depts;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
               console.log(res);
              if (res.code === 0)
                 depts = res.data
                // depts = res.data.filter((dept) => dept.type === "government");
              else depts = [];
              return res;
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);

              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.sign = null;
              this.depts = depts;
              this.handlers = [];
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.relayOrderTrack("validate");
              this.$refs.orderTrackRelayForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        error = error || (track.signs.length > 0 ? "" : "请先添加转派！");
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-sign-delete") {
        let sign = data;
        let index = track.signs.indexOf(sign);
        if (index > -1) track.signs.splice(index, 1);
        this.relayOrderTrack("validate");
      } else if (action === "track-sign-start") {
        let sign = data;
        if (sign) {
          this.sign = deepCopy(sign);
          this.$refs.orderTrackRelayFormSign.init();
          this.$nextTick(() => this.relayOrderTrack("track-sign-dept"));
        } else {
          this.sign = this.plugins.user.formatTrackSign();
          console.log(this.sign);
          this.$refs.orderTrackRelayFormSign.init();
          this.$nextTick(() => this.relayOrderTrack("track-sign-validate"));
        }
      } else if (action === "track-sign-validate") {
        let error = "";
        sign.dept.id = string(
          () => depts.find((dept) => dept.id === sign.dept.id).id
        );
        sign.dept.name = string(
          () => depts.find((dept) => dept.id === sign.dept.id).name
        );
        error = error || (sign.dept.id ? "" : "请先选择转派给哪个职能部门！");
        sign.handler.id = string(
          () => handlers.find((handler) => handler.id === sign.handler.id).id
        );
        sign.handler.name = string(
          () => handlers.find((handler) => handler.id === sign.handler.id).name
        );
        error = error || (sign.handler.id ? "" : "请先选择接收人！");
        sign.content = string(sign.content).trim();
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-sign-dept") {
        this.loading = true;
        // console.log(sign.dept);
        this.plugins.user.getOrderDeptDepments(sign.dept.id).then((res) => {
          this.loading = false;
          // console.log(res);
          if (res.code === 0) {
            this.handlers = res.data.reduce((clients, depment) => {
              depment.client.flag = depment.client.name + " - " + depment.duty;
              // if (depment.role === "manager" && depment.client.banned === "not")
              // console.log(depment.deptRole);
              if (depment.deptRole == "2") {
                clients.push(depment.client);
              }
              return clients;
            }, []);
            this.relayOrderTrack("track-sign-validate");
          } else {
            sign.dept.id = "";
            sign.handler.id = "";
            this.handlers = [];
            this.relayOrderTrack("track-sign-validate");
            this.plugins.modal.toast(
              "加载失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "track-sign-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            sign.files.push(file);
            this.relayOrderTrack("validate");
          });
      } else if (action === "track-sign-file-delete") {
        let file = data;
        let index = sign.files.indexOf(file);
        if (index > -1) sign.files.splice(index, 1);
      } else if (action === "track-sign-submit") {
        if (!this.relayOrderTrack("track-sign-validate", true)) return;
        if (sign.id) {
          let index = track.signs.findIndex((data) => data.id === sign.id);
          if (index > -1) track.signs.splice(index, 1, sign);
        } else {
          sign.id = "" + Date.now();
          track.signs.push(sign);
        }
        this.relayOrderTrack("validate");
        this.relayOrderTrack("track-sign-end");
      } else if (action === "track-sign-end") {
        this.$refs.orderTrackRelayFormSign.close();
      } else if (action === "submit") {
        if (!this.relayOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.relayOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.relayOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackRelayForm.close();
      }
    },
    revokeOrderTrackSign() {
      if (this.loading) return;
      if (!this.relaying) return;
      let order = this.order;
      this.plugins.modal
        .alert("撤回确认", "确定撤回转派吗？", "warning", [
          "确定撤回",
          "再想想",
        ])
        .then((ok) => {
          if (!ok) return;
          this.loading = true;
          this.plugins.user
            .revokeOrderTrackSign(order, order.tracks[0])
            .then((res) => {
              this.loading = false;
              if (res.code === 0) {
                this.plugins.modal.toast("操作成功！", "success");
                this.init();
              } else {
                this.plugins.modal.alert(
                  "操作失败",
                  res.code + "：" + res.msg,
                  "error"
                );
              }
            });
        });
    },
    replyOrderTrackSign(action, data) {
      if (this.loading) return;
      let profile = this.profile;
      let order = this.order;
      let track = this.track;
      let sign = this.sign;
      if (!action || action === "start") {
        this.track = order.tracks[0];
        this.sign = order.tracks[0].signs.find(
          (sign) => sign.finished === "not" && sign.handler.id === profile.id
        );
        this.replyOrderTrackSign("validate");
        this.$refs.orderTrackSignReplyForm.init();
      } else if (action === "validate") {
        let error = "";
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "sign-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            sign.replyFiles.push(file);
            this.replyOrderTrackSign("validate");
          });
      } else if (action === "sign-file-delete") {
        let file = data;
        let index = sign.replyFiles.indexOf(file);
        if (index > -1) sign.replyFiles.splice(index, 1);
      } else if (action === "submit") {
        if (!this.replyOrderTrackSign("validate", true)) return;
        this.loading = true;
        this.plugins.user.replyOrderTrackSignAdmin(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.replyOrderTrackSign("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackSignReplyForm.close();
      }
    },
    replyOrderTrack(action, data) {
      if (this.loading) return;
      let order = this.order;
      let track = this.track;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDeptDepments(
                order.dept
              );
              if (res.code === 0)
                handlers = res.data.reduce((clients, depment) => {
                  depment.client.flag =
                    depment.client.name + " - " + depment.duty;
                  if (
                    depment.role === "manager" &&
                    depment.client.banned === "not"
                  )
                    clients.push(depment.client);
                  return clients;
                }, []);
              else handlers = [];
              return res;
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.track.handler = this.plugins.user.formatClient();
              this.handlers = handlers;
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.replyOrderTrack("validate");
              this.$refs.orderTrackReplyForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        error = error || (track.handler.id ? "" : "请先选择接收人！");
        track.content = string(track.content).trim();
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.replyOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.replyOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.replyOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.replyOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackReplyForm.close();
      }
    },
    reviewOrderTrack(action, data) {
      if (this.loading) return;
      if (!this.reviewing) return;
      let order = this.order;
      let track = this.track;
      let handlers = this.handlers;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.track.handler.id = order.handler.id;
              this.handlers = [order.handler];
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.reviewOrderTrack("validate");
              this.$refs.orderTrackReviewForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        error = error || (track.handler.id ? "" : "请先选择接收人！");
        track.content = string(track.content).trim();
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.reviewOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.reviewOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.reviewOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.reviewOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackReviewForm.close();
      }
    },
    returnOrderTrack(action, data) {
      if (this.loading) return;
      //if (!this.finishing) return;
      let order = this.order;
      let track = this.track;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.returnOrderTrack("validate");
              this.$refs.orderTrackReturnForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        track.content = string(track.content).trim();
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.returnOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.returnOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.returnOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.returnOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackReturnForm.close();
      }
    },
    finishOrderTrackybc() {
      this.loading = true;

      this.plugins.user.postworkOrderSucceed(this.orderId).then((res) => {
        this.loading = false;
        if (res.code === 0) {
          this.plugins.modal.toast("操作成功！", "success");
          this.finishOrderTrack("end");
          this.init();
        } else {
          this.plugins.modal.toast(
            "操作失败<br>" + res.code + "：" + res.msg,
            "error"
          );
        }
      });
    },
    finishOrderTrack(action, data) {
      if (this.loading) return;
      if (!this.finishing) return;
      let order = this.order;
      let track = this.track;
      let offices = this.offices;
      if (!action || action === "start") {
        this.loading = true;
        this.plugins.user
          .call(
            async () => {
              if (order.tracks.length > 0) track = deepCopy(order.tracks[0]);
              else track = this.plugins.user.formatTrack();
              return { code: 0 };
            },
            async () => {
              let res = await this.plugins.user.getOrderDepts(order);
              if (res.code === 0)
                offices = res.data.filter(
                  (dept) =>
                    dept.type === "office" &&
                    dept.province.id === order.dept.province.id &&
                    dept.city.id === order.dept.city.id &&
                    dept.county.id === order.dept.county.id
                );
              else offices = [];
              for (let office of offices)
                await this.plugins.user
                  .getOrderDeptDepments(office)
                  .then((res) => {
                    if (res.code === 0)
                      office.depments = res.data.filter(
                        (depment) => depment.client.banned === "not"
                      );
                    else office.depments = [];
                  });
              return res;
            }
          )
          .then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.track = track;
              this.offices = offices.filter(
                (office) => office.depments.length > 0
              );
              this.finishOrderTrack("validate");
              this.$refs.orderTrackFinishForm.init();
            } else {
              this.plugins.modal.toast(
                "加载失败<br>" + res.code + "：" + res.msg,
                "error"
              );
            }
          });
      } else if (action === "validate") {
        let error = "";
        track.content = string(track.content).trim();
        error = error || (track.content ? "" : "请先输入办结意见！");
        track.copytos = offices
          .map((office) => office.depments)
          .flat()
          .reduce((copytos, depment) => {
            let index = copytos.findIndex(
              (copyto) => copyto.id === depment.client.id
            );
            if (index === -1) depment.selected && copytos.push(depment.client);
            return copytos;
          }, []);
        if (data) this.plugins.modal.toast(error, "error");
        return !error;
      } else if (action === "track-file-upload") {
        let type = data;
        if (type)
          this.uploadFile(type, null, (file) => {
            track.files.push(file);
            this.finishOrderTrack("validate");
          });
      } else if (action === "track-file-delete") {
        let file = data;
        let index = track.files.indexOf(file);
        if (index > -1) track.files.splice(index, 1);
      } else if (action === "submit") {
        if (!this.finishOrderTrack("validate", true)) return;
        this.loading = true;
        this.plugins.user.finishOrderTrack(order, track).then((res) => {
          this.loading = false;
          if (res.code === 0) {
            this.plugins.modal.toast("操作成功！", "success");
            this.finishOrderTrack("end");
            this.init();
          } else {
            this.plugins.modal.toast(
              "操作失败<br>" + res.code + "：" + res.msg,
              "error"
            );
          }
        });
      } else if (action === "end") {
        this.$refs.orderTrackFinishForm.close();
      }
    },
    revokeOrderTrack() {
      if (this.loading) return;
      if (!this.revoking) return;
      let order = this.order;
      this.plugins.modal
        .alert("撤回确认", "确定撤回吗？", "warning", ["确定撤回", "再想想"])
        .then((ok) => {
          if (!ok) return;
          this.loading = true;
          this.plugins.user
            .revokeOrderHolderTrack(order, order.tracks[0])
            .then((res) => {
              this.loading = false;
              if (res.code === 0) {
                this.plugins.modal.toast("操作成功！", "success");
                this.init();
              } else {
                this.plugins.modal.alert(
                  "操作失败",
                  res.code + "：" + res.msg,
                  "error"
                );
              }
            });
        });
    },
    delOrder() {
      if (this.loading) return;
      if (!this.deleting) return;
      let order = this.order;
      this.plugins.modal
        .alert("删除确认", "确定删除该事项吗？", "warning", [
          "确定删除",
          "再想想",
        ])
        .then((ok) => {
          if (!ok) return;
          this.loading = true;
          this.plugins.user.delOrder(order).then((res) => {
            this.loading = false;
            if (res.code === 0) {
              this.plugins.modal.toast("删除成功！", "success");
              this.$router.replace("/work/orders/type0");
            } else {
              this.plugins.modal.alert(
                "删除失败",
                res.code + "：" + res.msg,
                "error"
              );
            }
          });
        });
    },
    update() {
      if (this.loading) return;
      let order = this.order;
      let profile = this.profile;
      let track = this.order.tracks[0] || this.plugins.user.formatTrack();
      let trackback = this.order.tracks[1] || this.plugins.user.formatTrack();
      console.log(order, "111");
      console.log(trackback);
      // 当创建人与登录的人一样，并且事项在受理中、事项未办结时可以进行修改、删除
      this.editing =
        order.creater === profile.id &&
        ((track.type === "accept" && track.finished === "not") ||
          order.status == "return");
      this.starting =
        track.type === "start" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.accepting =
        track.type === "accept" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.uping =
        track.type === "accept" &&
        track.finished === "not" &&
        track.handler.id === profile.id &&
        order.level < 3;
      this.downing =
        track.type === "accept" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.checking =
        track.type === "check" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.handling =
        track.finished === "not" &&
        track.handler.id === profile.id
        // &&
        // track.type == "accept";

      this.handling2 =
        track.handler.id === profile.id &&
        track.type == "finish" &&
        trackback.isAdmin === "1";
      this.relaying =
        track.type === "relay" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.reviewing =
        track.type === "review" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.finishing =
        track.type === "finish" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.sucesssbcqk =
        track.type === "finish" &&
        track.finished === "yes" &&
        track.handler.id === profile.id;
      this.revoking =
        track.type == "relay" &&
        track.finished === "not" &&
        track.handler.id === profile.id;
      this.returning =
        track.type !== "relay" &&
        track.handler.id === profile.id &&
        track.finished === "not" &&
        trackback.type !== "up";
      this.deleting =
        order.creater === profile.id &&
        ((track.type === "accept" && track.finished === "not") ||
          order.status == "return");
    },
    destroy() {
      if (this.loading) return;
      return true;
    },
    init() {
      if (this.loading) return;
      let query = this.$route.query;
      let order = null;
      this.loading = true;
      this.orderId = query.orderId;
      this.order = cache(query.orderId);
      this.$root.$refs.navbar.init();
      this.plugins.user
        .call(
          async () => {
            let res = await this.plugins.user.init();
            if (res.code === 0) {
            }
            return res;
          },
          async () => {
            if (this.access(this.$route.name)) return { code: 0 };
            return this.env.xhrErrors.find((error) => error.code === 4);
          },
          async () => {
            let res = await this.plugins.user.getOrder(query.orderId);
            console.log(res);
            this.bcqk = res.data.bcqk;
            this.cenji = res.data.level;
            if (res.code === 0) order = res.data;
            if (res.code === 0) {
              for (let track of order.tracks) {
                let handler = track.handler;
                let depments = handler.depments.splice(0);
                for (let depment of depments)
                  depment.dept.id === track.dept.id &&
                    handler.depments.push(depment);
                let creator = order.creator;
                let matched =
                  track.type === "start" &&
                  track.handler.id === creator.id &&
                  creator.role === "holder";
                if (matched) order.creator = track.handler;
              }
              for (let creator of [order.creator, order.evaluatedCreator]) {
                let employees = creator.employees.splice(0);
                for (let employee of employees)
                  employee.company.id === order.company.id &&
                    creator.employees.push(employee);
              }
            }
            return res;
          }
        )
        .then((res) => {
          this.loading = false;
          this.$root.$refs.navbar.init();
          if (res.code === 0) {
            this.order = cache(query.orderId, order);
            this.track = null;
            this.update();
          } else if (res.code === 2) {
            this.$root.$refs.navbar.login();
          } else {
            this.plugins.modal
              .alert("页面加载失败", res.code + "：" + res.msg, "error")
              .then(() => this.$router.replace("/work/orders/type0"));
          }
        });
    },
  },
  created() {
    this.init();
  },
};
</script>

<style lang="scss">
.page-work-order {
  overflow: hidden;
}
</style>
